package com.xiaocikuai.smallchunks.service.Transaction;

import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

@Service
public class RandomCode {

    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    public static String getUniqueCode(int id) {
        Random random = new Random(id);
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            if (random.nextBoolean()) {
                // 生成数字
                code.append(random.nextInt(10));
            } else {
                // 生成字母（A-Z，a-z）
                int type = random.nextInt(2);
                if (type == 0) {
                    code.append((char) (random.nextInt(26) + 'A'));
                } else {
                    code.append((char) (random.nextInt(26) + 'a'));
                }
            }
        }
        return code.toString();
    }

    public String headRandom() {
        Random random = new Random();
        int num = 10 + random.nextInt(9);  // 生成 10 到 19 之间的随机数
        return String.valueOf(num);
    }

    public String getTimeCode(){
        Date date = new Date();
        return outputFormat.format(date);
    }

    public static String getUniqueNum(int id) {
        Random random = new Random(id);
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 2; i++) {  // 只循环 2 次，生成两位数字
            code.append(random.nextInt(10));  // 生成 0 到 9 的随机数字
        }
        return code.toString();
    }

    public String getOrderCode(int uid,int tid){
        return headRandom()+getTimeCode()+getUniqueCode(uid)+getUniqueNum(tid);
    }

    public String getLoginUnique() {
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            if (random.nextBoolean()) {
                // 生成数字
                code.append(random.nextInt(10));
            } else {
                // 生成字母（A-Z，a-z）
                int type = random.nextInt(2);
                if (type == 0) {
                    code.append((char) (random.nextInt(26) + 'A'));
                } else {
                    code.append((char) (random.nextInt(26) + 'a'));
                }
            }

        }
        return code.toString();
    }


}
